<html>
<head>
    <title>layui多文件上传</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="css/layui.css"  media="all">
</head>
<body>
<div style="display: none;" class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo">
    <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
</div>
<div class="layui-upload">
    <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
    <div class="layui-upload-list">
        <table class="layui-table">
            <thead>
            <tr><th>文件名</th>
                <th>大小</th>
                <th>上传进度</th>
                <th>状态</th>
                <th>操作</th>
            </tr></thead>
            <tbody id="demoList"></tbody>

        </table>
    </div>
    <button type="button" class="layui-btn" id="testListAction">开始上传</button>
</div>

<script src="layui.js" charset="utf-8"></script>
<script src="jquery-3.2.1.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['upload','element','layer'], function(){
        var $ = layui.jquery
            ,upload = layui.upload,
            element = layui.element,
			layer = layui.layer;
        var thisFileArray = new Array(),thisFileIndex = 0;//正在上传的文件
        var updataResult = [0,0,0];//上传的结果集 第一个表示总文件数 第二个表示上传成功数 第三个表示上传失败数
        //多文件列表示例
        var demoListView = $('#demoList')
            ,uploadListIns = upload.render({
            elem: '#testList'
            ,url: 'http://127.0.0.1:8003/common/wordToPdf'
            ,accept: 'file'//指定上传类型
            ,multiple: true//是否允许多文件上传 不支持ie8/9
			,number:1
            ,auto: false//是否选完文件后自动上传
            ,bindAction: '#testListAction'//指向一个按钮触发上传
			,data:{
				id: 11
			}
			,exts: 'doc|docx|uot'
			,accept :"file"
            ,choose: function(obj){

                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
				//console.log(files)
				//console.log(files.length)
				if(files.length > 1){
					layer.alert("只能上传一个文件", {icon: 2})
				}
                //读取本地文件
                obj.preview(function(index, file, result){
                    thisFileArray.push(index);
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td>' +
                        '<div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="progress_'+index+'">\n' +
                        '    <div class="layui-progress-bar" lay-percent="0%"><span class="layui-progress-text"></span></div>\n' +
                        '</div>' +
                        '</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));
                    //单个重传
                    tr.find('.demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });
                    //删除
                    tr.find('.demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
						//删除thisFileArray的对应元素
						var arrayIndex = thisFileArray.indexOf(index);
						thisFileArray.splice(index,1);

                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    demoListView.append(tr);
                });
            }
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                updataResult[0]+=obj.total;updataResult[1]+=obj.successful;updataResult[2]+=obj.aborted;
				//console.log("共选择:"+updataResult[0]+"个文件,上传成功:"+updataResult[1]+",上传失败:"+updataResult[2])
            }
            ,done: function(res, index, upload){
                if(res.status == 0000){ //上传成功

					console.log(res.fileData1)

					window.open("http://localhost:8004/"+res.fileData1)

                    element.progress('demo', (thisFileIndex/thisFileArray.length*100).toFixed(2)+"%"); //可配合 layui 进度条元素使用
					var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(3).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(4).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
				window.location.href = "../files/test.pdf"
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            ,progress: function(n){
                var percent = n + '%'; //获取进度百分比
                //console.log(thisFileArray[thisFileIndex]+"/"+percent);
                if (n==100) {
                    element.progress('progress_' + thisFileArray[thisFileIndex] + '', percent); //可配合 layui 进度条元素使用
                    thisFileIndex++;
                }else{
                    element.progress('progress_' + thisFileArray[thisFileIndex] + '', percent); //可配合 layui 进度条元素使用

                }
            }
        });

    });

	$("#test").click(function(){

	})
</script>

</body>
</html>
